import { createStore, applyMiddleware } from 'redux'
import { combineReducers } from 'redux-immutable'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'

let modules = {}
// 参1：指定的遍历目录，参2：是否递归进行子目录查询，参3：遍历的文件类型
let moduleFn = require.context('./reducers', false, /\.js$/)
// 返回一个函数，此函数中有对应的一个静态方法，keys()返回遍历出来的文件列表，数组
moduleFn.keys().forEach(file => {
  let obj = moduleFn(file).default
  let name = file.match(/\.\/(\w+)\.js$/)[1]
  modules[name] = obj
})

// 合并为一大的reducer
let reducer = combineReducers(modules)

export default createStore(reducer, composeWithDevTools(applyMiddleware(thunk)))
